import _ from 'lodash';
const { NativeModules } = require("react-native");
import moment from "moment";
const { XLogLibraryModule } = NativeModules;

function identifyDebugDevelopmentEnvironment() {
    try{
        const { scriptURL } = NativeModules.SourceCode
        if(scriptURL && scriptURL.length > 0) {
            const devSplit = scriptURL.split('&');
            if(devSplit.length > 1) {
                return devSplit[1] == 'dev=true';
            }
        }
        return false;
    }catch(error) {
    }
    return false;
}


export default class XLog{
    static i(...params) {
        try{
            let msg = this.printParams(params);
            if(identifyDebugDevelopmentEnvironment()) {
                console.log(moment().format("MM-DD HH:mm:ss"), msg);
            } else {
                XLogLibraryModule.i(msg);
            }
        }catch(error) {
            console.log("log i error:",error);
        }
    }
    static d(...params) {
        try{
            let msg = this.printParams(params);
            if(identifyDebugDevelopmentEnvironment()) {
                console.log(moment().format("MM-DD HH:mm:ss"), msg);
            } else {
                XLogLibraryModule.d(msg);
            }
        }catch(error) {
            console.log("log d error:",error);
        }
    }
    static e(...params) {
        try{
            let msg = this.printParams(params);
            if(identifyDebugDevelopmentEnvironment()) {
                console.log(moment().format("MM-DD HH:mm:ss"), msg);
            } else {
                XLogLibraryModule.e(msg);
            }
        }catch(error) {
            console.log("log e error:",error);
        }
    }

    static f(...params) {
        try{
            let msg = this.printParams(params);
            if(identifyDebugDevelopmentEnvironment()) {
                console.log(moment().format("MM-DD HH:mm:ss"), msg);
            } else {
                XLogLibraryModule.f(msg);
            }
        }catch(error) {
            console.log("log e error:",error);
        }
    }

    static printParams(...params) {
        let msg = "";
        for(let item of params) {
            if(_.isObjectLike(item)) {
                msg += JSON.stringify(item);
            } else {
                msg += item;
            }
        }
        return msg;
    }

    static flush() {
        XLogLibraryModule.flush();
    }

    static getLogPath() {
        return XLogLibraryModule.getLogPath();
    }

    static getCachePath() {
        return XLogLibraryModule.getCachePath();
    }

    // public static final int LEVEL_VERBOSE = 0;
    // public static final int LEVEL_DEBUG = 1;
    // public static final int LEVEL_INFO = 2;
    // public static final int LEVEL_WARNING = 3;
    // public static final int LEVEL_ERROR = 4;
    // public static final int LEVEL_FATAL = 5;
    // public static final int LEVEL_NONE = 6;
    static getLogLevel() {
        return XLogLibraryModule.getLogLevel();
    }

    static setLogLevel(level) {
        XLogLibraryModule.setLogLevel(level);
    }
}